import { HttpClient } from '@angular/common/http'
import { Injectable } from '@angular/core'
import { Observable } from 'rxjs'
import {
  QueryParams,
  UserParams,
  UserId,
  CollectionPublish
} from '../@core/interface/manage'

@Injectable({
  providedIn: 'root'
})
export class ManageService {
  constructor(private http: HttpClient) { }

  collectionAdd(collectionName, userId): Observable<any> {
    return this.http.post('v1/collection/add', {
      collectionName: collectionName,
      userId: userId
    })
  }

  collectionQuery(object: QueryParams): Observable<any> {
    const obj = {}
    Object.keys(object).forEach((item) => {
      if (item == 'collectionStatus') {
        obj['collectionStatus'] = object[item]
      } else if (object[item]) {
        obj[item] = object[item]
      }
    })
    return this.http.get('v1/collection/query', { params: obj })
  }

  // 集合修改
  collectionModify(obj: CollectionPublish): Observable<any> {
    return this.http.put('v1/collection/modify', {
      subCollectionIndex: obj.subCollectionIndex,
      userId: obj.userId,
      collectionIndex: obj.collectionIndex,
      collectionName: obj.collectionName,
      saleTime: obj.saleTime,
      offShelfTime: obj.offShelfTime,
      price: obj.price
    })
  }

  collectionPublish(obj: CollectionPublish): Observable<any> {
    return this.http.put('v1/collection/publish', {
      collectionIndex: obj.collectionIndex,
      saleTime: obj.saleTime,
      offShelfTime: obj.offShelfTime,
      price: obj.price,
      userId: obj.userId,
      subCollectionIndex: obj.subCollectionIndex
    })
  }

  // 用户列表
  getUserList(object: UserParams): Observable<any> {
    const obj = {}
    Object.keys(object).forEach((item) => {
      if (item == 'locked' && object[item] !== null) {
        obj['locked'] = object[item]
      } else if (object[item]) {
        obj[item] = object[item]
      }
    })
    return this.http.get('/v1/admin/userlist', { params: obj })
  }

  // 根据ID获取用户列表
  getUserListBaseOnID(object: UserId): Observable<any> {
    const obj = {}
    Object.keys(object).forEach((item) => {
      obj[item] = object[item]
    })
    return this.http.get('/v1/admin/user', { params: obj })
  }

  // 根据ID删除用户
  deleteUser(object: UserId): Observable<any> {
    const obj = {}
    Object.keys(object).forEach((item) => {
      obj[item] = object[item]
    })
    return this.http.delete('/v1/admin/user', { params: obj })
  }

  // 根据ID锁定用户
  lockUser(obj: UserId): Observable<any> {
    return this.http.put('v1/admin/user/lock', { userid: obj.userid })
  }

  // 根据ID解锁用户
  unlockUser(obj: UserId): Observable<any> {
    return this.http.put('v1/admin/user/unlock', { userid: obj.userid })
  }

  // 可见集合名称列表
  collectionNames(): Observable<any> {
    return this.http.get('v1/collection/names')
  }

  // 获取不可见集合
  collectionNamesInvisible(): Observable<any> {
    return this.http.get('v1/collection/names/invisible')
  }

  // 图片上传
  upLoadImg(formData: FormData): Observable<any> {
    return this.http.post('v1/file/save', formData)
  }

  // 铸造
  chainMint(userId: Number, formData: Array<any>): Observable<any> {
    return this.http.post('v1/chain/mint', { userId: userId, mintData: formData })
  }

  // 获取首页推荐展示信息
  collectionRecommend(num: number): Observable<any> {
    return this.http.get('v1/collection/recommend?num=' + num)
  }

  //获取集合详情
  collectionDetail(collectionIndex: string, subCollectionIndex: number): Observable<any> {
    return this.http.get('v1/collection/publish/detail', { params: { collectionIndex: collectionIndex, subCollectionIndex: subCollectionIndex } })
  }

  //结束出售
  collectionEnd(collectionIndex: string): Observable<any> {
    return this.http.put('/v1/collection/end', { collectionIndex: collectionIndex })
  }

  //藏品销毁
  collectionBurn(userId: number, collectionIndex: string, subCollectionIndex: number, tokenId?: string): Observable<any> {
    return this.http.post('/v1/chain/burnasset', {
      userId: userId,
      collectionIndex: collectionIndex,
      subCollectionIndex: subCollectionIndex,
      tokenId: tokenId
    }
    )
  }
}
